//
//  CCCarCorcleTools.m
//  CarCircle
//
//  Created by chenfangkun on 2019/1/11.
//  Copyright © 2019 chenfangkun. All rights reserved.
//

#import "CCCarCorcleTools.h"

@implementation CCCarCorcleTools

/// 封装有bodyPOST请求
+ (void)POSTRequestWithURL:(NSString *)url Paramerter:(NSDictionary *)paramerter CompletionHandler:(void (^)(id _Nonnull, NSError * _Nonnull))completionHandler {
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:paramerter options:0 error:&error];
    NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSString *urlStr = [NSString stringWithFormat:@"http:www/baidu%@",url];
    
    NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer]requestWithMethod:@"POST" URLString:urlStr parameters:nil error:nil];
    // 请求时长
    request.timeoutInterval = 30;
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"iOS" forHTTPHeaderField:@"platform"];
    
    NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:Ktoken];
    if (token.length != 0) {
        [request setValue:token forHTTPHeaderField:@"token"];
    }
    [request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    
    [[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (completionHandler) {
            completionHandler(responseObject,error);
        }
    }] resume];
}

/// 封装GET请求
+ (void)GETRequestWithURL:(NSString *)url Parameter:(NSDictionary *)parameter CompletionHandler:(void (^)(id responseObj, NSError * error))completionHandler {
    NSString *httpStr = [NSString stringWithFormat:@"http:www.baidu.com%@",url];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:Ktoken];
    if (token.length != 0) {
        [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
    }
    [manager.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"platform"];
    [manager GET:httpStr parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (completionHandler) {
            completionHandler(responseObject,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (completionHandler) {
            completionHandler(nil,error);
        }
    }];
}

/// 封装上传图片
+ (void)uploadImageRequestWithURL:(NSString *)url image:(NSArray<NSData *> *)imageList CompletionHandler:(void (^)(id responseObj, NSError * error))completionHandler {
    NSString *httpStr = [NSString stringWithFormat:@"http:www.baidu%@",url];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:httpStr]];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Contrnt-Type"];
    
    /// 加入请求头
    NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:Ktoken];
    if (token.length != 0) {
        [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
    }
    [manager.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"platform"];
    
    for (NSInteger i = 0; i < imageList.count; i++) {
        // 取出单张图片二进制数据
        NSData *imageData = imageList[i];
        // 在网络开发中，上传图片文件是，是文件不允许被覆盖，文件重名，要解决问题，可以在上传是使用当前的系统时间作为文件名
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        // 设置时间格式
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.png",str];
        NSDictionary *dict = @{};
        [manager POST:httpStr parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/png"];
        } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (completionHandler) {
                completionHandler(responseObject,nil);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (completionHandler) {
                completionHandler(nil,error);
            }
        }];
    }
}

@end
